package org.jeecg.modules.iot.mqtt.server.service.processor.carOperate;

import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.iot.model.Jt808Control.Jt808CentralControlConfig;
import org.jeecg.modules.iot.mqtt.server.service.processor.JT808Processor;
import org.jeecg.modules.iot.mqtt.server.service.processor.MessageType;
import org.jeecg.modules.rental.entity.RentalEBike;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;


@Service
public class CarService {
    @Autowired
    private JT808Processor jt808Processor;

    /**
     * Open car functionality.
     *
     * @param car The RentalEBike object containing car details.
     * @return Result indicating the status of the operation.
     */
    /*上电*/
    public Boolean openCar(RentalEBike car) throws ExecutionException, InterruptedException, TimeoutException {
        // 使用消息类型"8500"调用处理器
        jt808Processor.process(car, MessageType.OPEN_CAR);
        Integer seq = jt808Processor.process(car, MessageType.OPEN_CAR);
        if (seq != null) {
            return true;
        } else {
            return false;
        }

    }

    public Boolean startLocationTrack(RentalEBike car, Short timeInterval, int validityPeriod) {
        Jt808CentralControlConfig config = new Jt808CentralControlConfig();
        config.setTimeInterval(timeInterval);
        config.setValidityPeriod(validityPeriod);

        Integer seq = jt808Processor.process(car, MessageType.LOCATION_TRACK, config);
        return seq != null; // 发送成功返回 true，否则返回 false
    }

    /*断电*/
    public Boolean closeCar(RentalEBike car) {
        // 使用消息类型"8500"调用处理器
        jt808Processor.process(car, MessageType.CLOSE_CAR);
        Integer seq = jt808Processor.process(car, MessageType.CLOSE_CAR);
        if (seq != null) {
            return true;
        } else {
            return false;
        }
    }

    /*还车*/
    public Boolean returnCar(RentalEBike car) {
        // 异步执行还车操作
        CompletableFuture.runAsync(() -> {
            try {
                // 播放还车语音
                setPlayAudio(car, (byte) 10); // 假设 10 是还车语音的索引

                // 延迟两秒
                TimeUnit.SECONDS.sleep(2);

                // 关闭车辆
                closeCar(car);
            } catch (InterruptedException e) {
                // 处理中断异常
                Thread.currentThread().interrupt();
            }
        });

        return true;
    }

    /*设防*/
    public Boolean defenseCar(RentalEBike car) {
        // 使用消息类型"8500"调用处理器
        Integer seq = jt808Processor.process(car, MessageType.DEFENSE_CAR);
        if (seq != null) {
            return true;
        } else {
            return false;
        }


    }

    /*设防*/
    public Boolean disarmingCar(RentalEBike car) {
        // 使用消息类型"8500"调用处理器
        Integer seq = jt808Processor.process(car, MessageType.DISARMING_CAR);
        if (seq != null) {
            return true;
        } else {
            return false;
        }
    }

    /*开龙头锁*/
    public Boolean handleLockOpen(RentalEBike car) {
        // 使用消息类型"8500"调用处理器
        Integer seq = jt808Processor.process(car, MessageType.HANDLE_LOCK_OPEN);
        if (seq != null) {
            return true;
        } else {
            return false;
        }


    }

    public Boolean setVolume(RentalEBike car, int volume) {
        if (volume < 0 || volume > 100) {
            throw new IllegalArgumentException("音量值必须在 0-100 之间");
        }
        Jt808CentralControlConfig config = new Jt808CentralControlConfig();
        config.setVolume(volume);
        Integer seq = jt808Processor.process(car, MessageType.SET_VOLUME, config);
        if (seq != null) {
            return true;
        } else {
            return false;
        }
    }

    /*关龙头锁*/
    public Boolean handleLockClose(RentalEBike car) {
        // 使用消息类型"8500"调用处理器
        Integer seq = jt808Processor.process(car, MessageType.HANDLE_LOCK_CLOSE);
        if (seq != null) {
            return true;
        } else {
            return false;
        }


    }
    /**
     * 遥控器配对
     * @param car 车辆信息
     * @return 操作结果
     */
    public Boolean pairRemoteControl(RentalEBike car) {
        Integer seq = jt808Processor.process(car, MessageType.REMOTE_CONTROL_PAIR);
        return seq != null;
    }


    /**
     * 清空遥控器
     * @param car 车辆信息
     * @return 操作结果
     */
    public Boolean clearRemoteControl(RentalEBike car) {
        Integer seq = jt808Processor.process(car, MessageType.CLEAR_REMOTE_CONTROL);
        return seq != null;
    }

    /*寻车*/
    public Boolean searchCar(RentalEBike car) {
        // 使用消息类型"8500"调用处理器
        Integer seq = jt808Processor.process(car, MessageType.SEARCH_CAR);
        if (seq != null) {
            return true;
        } else {
            return false;
        }
    }

    /*开座位锁*/
    public Boolean seatLock(RentalEBike car) {
        // 使用消息类型"8500"调用处理器
        Integer seq = jt808Processor.process(car, MessageType.SEAT_LOCK);
        if (seq != null) {
            return true;
        } else {
            return false;
        }
    }

    public Result queryLocation(RentalEBike car) {
        Integer seq = jt808Processor.process(car, MessageType.QUERY_LOCATION);
        if (seq != null) {
            return Result.OK();
        } else {
            return Result.error("位置查询失败");
        }

    }

    public Boolean setPlayAudio(RentalEBike car, Byte audioIndex) {
        Jt808CentralControlConfig config = new Jt808CentralControlConfig();
        config.setAudioIndex(audioIndex);
        Integer seq = jt808Processor.process(car, MessageType.PLAY_AUDIO, config);
        if (seq != null) {
            return true;
        } else {
            return false;
        }
    }

    public Result queryControl(String address) {

        return null;
    }
}
